import 'package:citiesocial/_include.dart';

/// 注册成功会返回一个‘1’
class Register extends BaseStatefulWidget {
  @override
  State<StatefulWidget> createState() => _RegisterState();
}

class _RegisterState extends BaseState<Register> {
  final _firstName = TextEditingController(),
      _lastName = TextEditingController(),
      _email = TextEditingController(),
      _code = TextEditingController(),
      _password = TextEditingController();

  void _getCode() async {
    final email = _email.text;
    if (email.length == 0) {
      Toast.show(context, '邮件地址不能为空');
      return;
    }
    if (!email.contains('@')) {
      Toast.show(context, '邮件格式不正确');
      return;
    }
    LoadingDialog.show(context);
    final code = await HttpManager.getVerify(email, context: context);
    await LoadingDialog.dismiss(context);
    if (code) {
      Toast.show(context, '验证码发送成功');
    }
  }

  void _register() async {
    final firstname = _firstName.text;
    if (firstname.length == 0) {
      Toast.show(context, '名不能为空');
      return;
    }
    final lastname = _lastName.text;
    if (lastname.length == 0) {
      Toast.show(context, '姓不能为空');
      return;
    }
    final email = _email.text;
    if (email.length == 0) {
      Toast.show(context, '邮件不能为空');
      return;
    }
    final code = _code.text;
    if (code.length == 0) {
      Toast.show(context, '邮件验证码不能为空');
      return;
    }
    final password = _password.text;
    if (password.length == 0) {
      Toast.show(context, '密码不能为空');
      return;
    }
    LoadingDialog.show(context);
    final deviceId = await getDeviceId();
    final user = await HttpManager.register(
        email, password, firstname, lastname, code, deviceId,
        context: context);
    await LoadingDialog.dismiss(context);
    if (user != null && user.user_id != null && user.user_id.length > 0) {
      Toast.show(context, '注册成功').then((_) {
        Configs.saveUserState(user);
        Navigator.pop(context, '1');
      });
    } else {
      Toast.show(context, '注册失败');
    }
  }

  void _loadUserAgreement() async {
    LoadingDialog.show(context);
    final agreement = await HttpManager.userAgreement(context);
    await LoadingDialog.dismiss(context);
    if (agreement != null &&
        agreement.link != null &&
        agreement.link.length > 0) {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (_) => WebviewScaffold(
                appBar: AppBarUtils.normal(context, agreement.title),
                url: '${agreement.link}',
              ),
        ),
      );
    }
  }

  @override
  void dispose() {
    _firstName.dispose();
    _lastName.dispose();
    _email.dispose();
    _code.dispose();
    _password.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        resizeToAvoidBottomPadding: false,
        appBar: AppBarUtils.normal(context, '注册'),
        backgroundColor: Colors.white,
        body: SafeArea(
          child: Container(
            color: Colors.white,
            child: Column(
              children: <Widget>[
                Expanded(
                  child: Column(
                    children: <Widget>[
                      Container(
                        decoration: ShapeDecoration(
                          shape: RoundedRectangleBorder(
                            side: BorderSide(color: Configs.main),
                            borderRadius:
                                BorderRadius.all(Radius.circular(8.0)),
                          ),
                        ),
                        padding: EdgeInsets.symmetric(horizontal: 10.0),
                        child: Column(
                          children: <Widget>[
                            TextFormField(
                              controller: _firstName,
                              maxLines: 1,
                              decoration: InputDecoration(
                                  hintText: '名', border: InputBorder.none),
                            ),
                            Configs.dividerLine,
                            TextFormField(
                              controller: _lastName,
                              maxLines: 1,
                              decoration: InputDecoration(
                                  hintText: '姓', border: InputBorder.none),
                            ),
                            Configs.dividerLine,
                            TextFormField(
                              controller: _email,
                              maxLines: 1,
                              decoration: InputDecoration(
                                  hintText: '电子邮件', border: InputBorder.none),
                            ),
                            Configs.dividerLine,
                            TextFormField(
                              controller: _code,
                              maxLines: 1,
                              decoration: InputDecoration(
                                  hintText: '邮件验证码', border: InputBorder.none),
                            ),
                            Configs.dividerLine,
                            TextFormField(
                              obscureText: true,
                              controller: _password,
                              maxLines: 1,
                              decoration: InputDecoration(
                                  hintText: '密码', border: InputBorder.none),
                            ),
                          ],
                        ),
                        margin: EdgeInsets.all(15.0),
                      ),
                      Container(
                        child: RaisedButton(
                          color: Configs.main,
                          textColor: Colors.white,
                          child: Text(
                            '获取验证码',
                            style: TextStyle(fontSize: 18.0),
                          ),
                          onPressed: _getCode,
                          padding: EdgeInsets.symmetric(vertical: 10.0),
                        ),
                        width: double.infinity,
                        margin: EdgeInsets.symmetric(horizontal: 15.0),
                      ),
                      Container(
                        child: RaisedButton(
                          color: Configs.main,
                          textColor: Colors.white,
                          child: Text(
                            '注册',
                            style: TextStyle(fontSize: 18.0),
                          ),
                          onPressed: _register,
                          padding: EdgeInsets.symmetric(vertical: 10.0),
                        ),
                        width: double.infinity,
                        margin: EdgeInsets.symmetric(
                            horizontal: 15.0, vertical: 5.0),
                      ),
                    ],
                  ),
                ),
                Container(
                  child: GestureDetector(
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Text(
                          '登录等同您同意',
                          style: TextStyle(fontSize: 12.0, color: Colors.grey),
                        ),
                        Text(
                          '<服务条款>',
                          style: TextStyle(fontSize: 12.0, color: Configs.main),
                        ),
                      ],
                    ),
                    onTap: _loadUserAgreement,
                  ),
                  margin: EdgeInsets.only(bottom: 10.0),
                ),
              ],
            ),
          ),
        ),
      );
}
